﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class OnlyAuthenticated_OnlyManagers_EditModele : System.Web.UI.Page
{

    String currentUser;
    SqlConnection connection = new SqlConnection("Server=27424b2d-be0f-46c5-b105-a1ab015ef95f.sqlserver.sequelizer.com;Database=db27424b2dbe0f46c5b105a1ab015ef95f;User ID=flvirntmiujloika;Password=wVBm2uXivJGURbWJuu7sJZJEpKRsuhbTDCJbPovZKR8g7aTsbUgnwhyEm65qKDWn;");

    protected void Page_Load(object sender, EventArgs e)
    {
        currentUser = LoginName.Page.User.Identity.Name;
        //TextBoxDenumireModel.Text = currentUser; 
        //TextBoxTextModel.Text = "";

        TextBoxDenumireModel0.Text = DropDownListModeleExistente.SelectedValue;

    }
    protected void AdaugaModelulButton_Click(object sender, EventArgs e)
    {

        if ((TextBoxTextModel.Text != "") & (TextBoxDenumireModel.Text != "") & (!TextBoxDenumireModel.Text.EndsWith(" ")) & (!TextBoxDenumireModel.Text.StartsWith(" ")))
        {
            string val = TextBoxTextModel.Text.Replace("\r\n", "<br />");//so that SQL gets the RETURN keys in text
            InsertRecord(TextBoxDenumireModel.Text, val, DropDownListSpecialitati.SelectedValue, currentUser);

            TextBoxDenumireModel.Text = "";
            TextBoxTextModel.Text = "";
            DropDownListSpecialitati.DataBind(); //force refresh
        }


    }



    private void InsertRecord(String numeModel, String textModel, String numeSpecialitate, String currentUser1)
    {

        //string sqlStatementWithCheck = "IF EXISTS (SELECT ID, NumeModel FROM DenumiriModele WHERE NumeModel = @numeModel) BEGIN SELECT ID FROM DenumiriModele END ELSE BEGIN INSERT INTO DenumiriModele (NumeModel, TextModel, Specialitate, UserEditor) VALUES (@numeModel, @textModel, @numeSpecialitate, @currentUser1) END";
        string sqlStatementWithCheck = "IF NOT EXISTS (SELECT ID, NumeModel FROM DenumiriModele WHERE NumeModel = @numeModel) BEGIN INSERT INTO DenumiriModele (NumeModel, TextModel, Specialitate, UserEditor) VALUES (@numeModel, @textModel, @numeSpecialitate, @currentUser1) END";

        //IF EXISTS (SELECT ID, NumeModel FROM DenumiriModele WHERE NumeModel = "bj") BEGIN END ELSE BEGIN INSERT INTO DenumiriModele (NumeModel, TextModel, Specialitate, UserEditor) VALUES ("bjx", "bjxxxx", "test", "bj") END
        //string sqlStatement = "INSERT INTO DenumiriModele (NumeModel, TextModel, Specialitate, UserEditor) VALUES (@numeModel, @textModel, @numeSpecialitate, @currentUser1)";

        try
        {
            connection.Open();
            SqlCommand cmd = new SqlCommand(sqlStatementWithCheck, connection);
            cmd.Parameters.AddWithValue("@NumeModel", numeModel);
            cmd.Parameters.AddWithValue("@TextModel", textModel);
            cmd.Parameters.AddWithValue("@NumeSpecialitate", numeSpecialitate);
            cmd.Parameters.AddWithValue("@currentUser1", currentUser1);
            //cmd.CommandType = CommandType.Text;
            cmd.ExecuteNonQuery();

            // Int32 rowsAffected = cmd.ExecuteNonQuery();
            // Console.WriteLine("RowsAffected: {0}", rowsAffected);


        }
        catch (System.Data.SqlClient.SqlException ex)
        {
            string msg = "Deletion/Insertion Error:";
            msg += ex.Message;
            throw new Exception(msg);
        }
        finally
        {
            connection.Close();
        }

    }


    protected void TextBoxTextModel1_TextChanged(object sender, EventArgs e)
    {

        //--this is for getting/setting values to/from components of an LoginView
        ContentPlaceHolder cp1 = (ContentPlaceHolder)Master.FindControl("MainContent");
        if (cp1 != null)
        {
            ContentPlaceHolder cp2 = (ContentPlaceHolder)Master.FindControl("GeneratorPageContent");
            if (cp2 != null)
            {
                Control ctl = (Control)LoginView3.FindControl("managers");
                if (ctl != null)
                {
                    Button but = (Button)ctl.FindControl("SaveButton");
                    //TextBox txtPwd = (TextBox)ctl.FindControl("Password");
                    if (but != null) but.Enabled = true;
                }
            }
        }
        //SaveButton.Enabled = true;
    }




    protected void DeleteButton_Click(object sender, EventArgs e)
    {
        deleteRecord(DropDownListModeleExistente.SelectedValue);
        TextBoxDenumireModel0.Text = "";
        DropDownListModeleExistente.DataBind(); //force refresh
    }

    protected void deleteRecord(String modelName)
    {
        string deleteQuery = "Delete from DenumiriModele WHERE [NumeModel] = @modelName";
        try
        {
            connection.Open();
            SqlCommand cmd = new SqlCommand(deleteQuery, connection);
            cmd.Parameters.AddWithValue("@modelName", modelName);
            cmd.ExecuteNonQuery();

            // Int32 rowsAffected = cmd.ExecuteNonQuery();
            // Console.WriteLine("RowsAffected: {0}", rowsAffected);


        }
        catch (System.Data.SqlClient.SqlException ex)
        {
            string msg = "Deletion/Insertion Error:";
            msg += ex.Message;
            throw new Exception(msg);
        }
        finally
        {
            connection.Close();
        }
    }




    protected void SaveButton_Click(object sender, EventArgs e)
    {
       // string textModel = <%# Eval("TextModel") %>;
      //  updateRecord(TextBoxDenumireModel0.Text,,DropDownListModeleExistente.SelectedValue);
        TextBoxDenumireModel0.Text = "";
        DropDownListModeleExistente.DataBind(); //force refresh


        //--this is for getting/setting values to/from components of an LoginView
        ContentPlaceHolder cp1 = (ContentPlaceHolder)Master.FindControl("MainContent");
        if (cp1 != null)
        {
            ContentPlaceHolder cp2 = (ContentPlaceHolder)Master.FindControl("GeneratorPageContent");
            if (cp2 != null)
            {
                Control ctl = (Control)LoginView3.FindControl("managers");
                if (ctl != null)
                {
                    Button but = (Button)ctl.FindControl("SaveButton");
                    //TextBox txtPwd = (TextBox)ctl.FindControl("Password");
                    if (but != null) but.Enabled = true;
                }
            }
        }
        
        //SaveButton.Enabled = false;
    }

    protected void updateRecord(String modelName, String modelText, String selectedModelName)
    {
        //string deleteQuery = "Delete from DenumiriModele WHERE [NumeModel] = @modelName";
        string updateQuery = "UPDATE DenumiriModele SET NumeModel = @modelName, TextModel = @modelText WHERE NumeModel = @selectedModelName";
        
        try
        {
            connection.Open();
            SqlCommand cmd = new SqlCommand(updateQuery, connection);
            cmd.Parameters.AddWithValue("@modelName", modelName);
            cmd.ExecuteNonQuery();

            // Int32 rowsAffected = cmd.ExecuteNonQuery();
            // Console.WriteLine("RowsAffected: {0}", rowsAffected);


        }
        catch (System.Data.SqlClient.SqlException ex)
        {
            string msg = "Deletion/Insertion Error:";
            msg += ex.Message;
            throw new Exception(msg);
        }
        finally
        {
            connection.Close();
        }
    }


}